import string
from random import randint, sample

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from App.models import User


def index(request):
    return render(request, 'index.html')


def delete(request, uid):
    try:
        user = User.objects.get(pk=2)
        if user:
            user.delete()
    except Exception as e:
        print(e)
    return HttpResponse('删除成功')


def add(request):
    users = []
    first = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳"
    last = ["雨莺",
            "筱萱",
            "刁丽",
            "莺拓",
            "刁洁",
            "蕴紫",
            "骖娅",
            "琼诗",
            "玲菲",
            "每缨",
            "陇髓",
            "莺彗",
            "邈钏",
            "二瑾",
            "鹦鹊",
            "灌雪",
            "茶薇",
            "藕妍",
            "苹苑",
            "碧岑",
            "琼云",
            "琼雅",
            "琼莎",
            "琼莉",
            "琼丹",
            "琼雪",
            "琼湄",
            "珏燕",
            "蕊滢",
            "霓镶",
            "莎欣",
            "婷怡",
            "荔妍",
            "二堇",
            "铛嫣",
            "尥芙",
            "戆琬",
            "劬香",
            "琳抑",
            "莺翰",
            "洫謦",
            "茜茱",
            "今薹",
            "雯篮",
            "聋莹",
            "聋葭",
            "钥一",
            "茹一",
            "祐颖",
            "莎萱",
            "璎葭",
            "蓝一",
            "馨丹",
            "篮一",
            "玉蓓",
            "岑姿",
            "遴丁",
            "瑰懿",
            "琵苻",
            "一兰",
            "苏识",
            "菩彤",
            "佑姝",
            "媛茜",
            "少婉",
            "月阑",
            "墨泉",
            "墨懿",
            "傲姿",
            "琅窝",
            "鑫琬",
            "篱璎",
            "璎云",
            "伺蔚",
            "娉梅",
            "卉桂",
            "琶婧",
            "瀚槿"]
    for _ in range(1000):
        username = list(first)[randint(0, len(first) - 1)] + list(last)[randint(0, len(last) - 1)]
        password = ''.join(sample(string.ascii_letters + string.digits, 10))
        users.append(User(username=username, password=password))
    User.objects.bulk_create(users)
    return HttpResponse("数据增加成功!")


def update(request):
    user = User.objects.get(pk=1)
    user.username = '小明'
    user.password = 'admin'
    user.save()
    return HttpResponse("数据修改成功!")


def show(request):
    user = User.objects.get(pk=1)
    return HttpResponse("用户信息" + user.username + "---" + user.password)


def query(request):
    # all 过滤器
    users = User.objects.filter()
    users = users.filter(uid__lt=20)  # uid<20



    return render(request, 'userlist.html', locals())
